<?php
namespace app\admin\controller;
use think\Controller;
use think\View;
use think\Db;
use think\Url;
use think\Request;
use app\admin\models\Admins;
use app\admin\models\Role;

class Admin extends Controller
{
	public function permission_add()
    {
    	if($_POST){
       		$role = new Role;
			$data = array(
				'role' => $_POST['role'],	
				'describe' => $_POST['describe'],				
				'dateline' => time()
			);	
			
			$rt=$role->insert($data);
			if($rt){
				return json(array('success'=>1,'info'=>'添加成功'));
			}else{
				return json(array('success'=>0,'info'=>'添加失败'));
			}

		}else{
			return $this->fetch('admin_permission_add');
			}			
		
	
	
	}
   
	
	public function permission()
    {	$role = new Role;
		$rol = $role->select();
		if($rol){
			return $this->fetch('admin_permission',['rol'=>$rol]);
		}
		
		
			 	
	}
public function permission_edit()
    {
    	$role = new Role;
    	if($_POST){   	
    		if(input('param.role_id')){
    			$role_id=input('param.role_id');	
    			$data = array(
				'role' => $_POST['role'],	
				'describe' => $_POST['describe'],				
				'dateline' => time()
			);	
    		$ret=$role->where('role_id',$role_id)->update($data);
			if($ret){
				return json(array('success'=>1,'info'=>'修改成功'));
			}else{
				return json(array('success'=>0,'info'=>'修改失败'));
			}

    		}

		}else{
			$rid = input('param.role_id');			
			$re = $role::get($rid);
			 return $this->fetch('admin_permission_add',['re'=>$re]);
			}			
	}

	public function permission_del()
     {	
     if($_POST){
     	$rid = input('param.role_id');
     	$rt=Role::destroy($rid);
     	if($rt){
				return json(array('success'=>1,'info'=>'删除成功'));
			}else{
				return json(array('success'=>0,'info'=>'删除失败'));
			}
     }

	}



	//管理员

	public function lists()
    {	$admin=new Admins;
    	$rol=$admin->select()->toArray();
    	$admins =Admins::all()->toArray();
		//$id=$admins['role'];
		// print_r($id);exit;
		foreach ($rol as $key => $v) {			
				$id=$v['role'];			
		}
		if(!$_POST){
			if($id){
				$role=new Role;
				$re=$role->where('role_id',$id)->find();
			}
			
			return $this->fetch('admin_list',['ad'=>$admins,'re'=>$re]);
		}else{
			echo json(['success'=>3]);
		}
	 
	
	}

	public function add()
    {if($_POST){
    	//print_r($_POST);exit;
    		$admins = new Admins;
			$data = array(
				'username' => $_POST['username'],	
				'password' => md5($_POST['password']),
				'sex' => $_POST['sex'],
				'tels' => $_POST['tels'],
				'emails' => $_POST['emails'],
				'role' => $_POST['role'],
				'dateline' => time()
			);	
			//print_r($data);exit;		
			$ret=$admins->insert($data);
			if($ret){
				return json(array('success'=>1,'info'=>'添加成功'));
			}else{
				return json(array('success'=>0,'info'=>'添加失败'));
			}

		}else{
			$role = new Role;
			$ro = $role->select();
			if($ro){
				return $this->fetch('admin_add',['ro'=>$ro]);
			}
			 
			}
		}			
	

	public function edit()
    {
    	$admins = new Admins;
    	if($_POST){   	
    		if(input('param.admin_id')){
    			$admin_id=input('param.admin_id');	
    			$data = array(
				'username' => $_POST['username'],	
				'password' => md5($_POST['password']),
				'sex' => $_POST['sex'],
				'tels' => $_POST['tels'],
				'emails' => $_POST['emails'],
				'role' => $_POST['role'],
				'dateline' => time()
			);	
    		$rets=$admins->where('admin_id',$admin_id)->update($data);
			if($rets){
				return json(array('success'=>1,'info'=>'修改成功'));
			}else{
				return json(array('success'=>0,'info'=>'修改失败'));
			}

    		}

		}else{

			$aid = input('param.admin_id');			
			$rse = $admins::get($aid);
			$rid=$rse->role;
			//print_r($rid);exit;
			if($rid){
				$role=new Role;
				$re=$role->where('role_id',$rid)->find();
			}
			
			 return $this->fetch('admin_add',['rse'=>$rse,'re'=>$re]);
			}			
	}

	public function del()
     {	
     if($_POST){
     	$aid = input('param.admin_id');
     	$vd=admins::destroy($aid);
     	if($vd){
				return json(array('success'=>1,'info'=>'删除成功'));
			}else{
				return json(array('success'=>0,'info'=>'删除失败'));
			}
     }

	}


	
}
?>